<?php

class Country extends AppModel {

    var $alias = 'Country';
    var $name = 'Country';
    var $useTable = 'area_countries';
    var $hasMany = array(
        'City' => array(
            'className' => 'City',
            'foreignKey' => 'country_id',
            'dependent' => true,
        )
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function getListAll() {
        $data = $this->find('list', array(
                    'fields' => 'Country.id, Country.title',
                ));
        return $data;
    }

    function write_list() {
        $dbrows = $this->find('all', array(
                    'fields' => 'id, title'
            )
        );
        $html       =   '<select class="list_country" name="list_country" id="list_country">' . "\n";
        $html       .=  '<option value="0">--Chọn quốc gia--</option>' . "\n";
        foreach($dbrows AS $row){
            $html   .=  '<option value="' . $row['Country']['id'] . '">' . $row['Country']['title'] . '</option>' . "\n";
        }
        $html       .=   '</select>' . "\n";
        $resourse   =   fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'list_country.ctp', 'w');
        fwrite($resourse, $html);
    }

}

?>
